package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Hospital;
import com.example.entity.Manager;
import com.example.entity.Manager;
import com.example.service.IManagerService;
import com.example.service.IManagerService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author wyx
 * @since 2025-03-10
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
@Slf4j
@Api(tags = "管理员管理模块")
public class ManagerController {
    @Autowired
    IManagerService ManagerService;

    //展示管理员列表
    @GetMapping("/getManagers")
    public List<Manager> Managerlist() {
        log.info("获取管理员列表");
        return ManagerService.list();
    }

    //分页查询管理员列表
    @RequestMapping("/page")
    public R managerpage(Integer page, Integer pageSize, String name) {
        log.info("分页查询医院列表page:{},pageSize:{}", page, pageSize);
        Page<Manager> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        if (name != null){
            queryWrapper.like(Manager::getName, name);
        }

        return R.success(ManagerService.page(pageInfo, queryWrapper));
    }


    //根据id查询管理员
    @GetMapping("/selectById")
    public Map<String, Object> Managerget(@RequestParam Integer id) {
        log.info("获取管理员{}", id);
        Manager Manager = ManagerService.getById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("admin", Manager);
        return result;
    }

    //新增管理员
    @PostMapping("/addadmin")
    public R<String> Manageradd( Manager newUserData) {
        log.info("新增管理员{}", newUserData);
        Boolean flag = ManagerService.save(newUserData);
        if (flag) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    //删除管理员
    @DeleteMapping("/deleteadmin/{id}")
    public R<String> Managerdelete(@PathVariable("id") Integer id) {
        log.info("删除管理员{}", id);
        Boolean flag = ManagerService.removeById(id);
        if (flag) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    //修改管理员
    @PutMapping("/updateadmin")
    public R<String> Managerupdate( Manager Manager) {
        log.info("修改管理员{}", Manager);
        Boolean flag = ManagerService.updateById(Manager);
        if (flag) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    //管理员登录
    @PostMapping("/login")
    public Map<String, Object> Managerlogin(@RequestParam String name,
                                          @RequestParam String password,
                                          HttpSession httpSession) {
        log.info("管理员登录，账号：{}，密码：{}", name, password);
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manager::getName, name).eq(Manager::getPassword, password);
        Manager Manager = ManagerService.getOne(queryWrapper);

        Map<String, Object> R = new HashMap<>();
        if (Manager != null) {
            R.put("flag", true);
            R.put("role", "manager"); // 返回管理员角色信息
            R.put("admin", Manager);
            httpSession.setAttribute("Manager", Manager.getId()); // 将管理员ID存入Session
        } else {
            R.put("flag", false);
            R.put("message", "账号或密码错误"); // 返回错误信息
        }
        log.info("R:{}", R.toString());
        return R; // 返回JSON对象
    }
}